#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <memory>
#include "thread.hpp"

uint16_t serverport = 0;
std::string serverip;

static void Usage(std::string proc)
{
    std::cout << "\nUsage" << proc << " ip port\n"
              << std::endl;
}

static void *sendUdp(void *args)
{
    int sock = *(int *)((threadData *)args)->args_;
    std::string name = ((threadData *)args)->name_;

    std::string message;
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    server.sin_port = htons(serverport);

    while (true)
    {
        std::cerr << "请输入你的信息#";
        std::getline(std::cin, message);
        if (message == "quit")
            break;
        // 当client首次发送消息给服务器的时候，OS会自动给client bind他的IP和PORT
        sendto(sock, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));
    }
    return nullptr;
}
static void *receUdp(void *args)
{
    int sock = *(int *)((threadData *)args)->args_;
    std::string name = ((threadData *)args)->name_;

    char buffer[1024];
    while(true)
    {
        memset(buffer,0,sizeof buffer);
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        ssize_t s = recvfrom(sock,buffer,sizeof buffer,0,(struct sockaddr*)&temp,&len);
        if(s > 0)
        {
            buffer[s] = 0;
            std::cout << buffer << std::endl;
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        std::cerr << "socket error" << std::endl;
        exit(2);
    }
    // client不会显示的bind，程序员不会自己bind
    // client是一个客户端 - 如果程序员自己bind了 - client一定bind了一个固定的ip和port
    // 万一其他客户端提前占用了这个port呢？
    // 所以client不需要显式的bind指定port，而是由操作系统随机分配
    serverip = argv[1];
    serverport = atoi(argv[2]);
    std::unique_ptr<Thread> sender(new Thread(1, sendUdp, (void *)&sock));
    std::unique_ptr<Thread> recver(new Thread(2, receUdp, (void *)&sock));
    sender->start();
    recver->start();

    sender->join();
    recver->join();

    close(sock);

    // struct sockaddr_in server;
    // memset(&server,0,sizeof server);
    // server.sin_family = AF_INET;
    // server.sin_port = htons(atoi(argv[2]));
    // server.sin_addr.s_addr = inet_addr(argv[1]);
    // char buffer[1024];

    // while(true)
    // {
    //     std::cerr << "请输入你的信息# "; //标准错误 2打印
    //     std::string message;
    //     std::getline(std::cin,message); // 不输入就会一直卡在那
    //     if(message == "quit") break;
    //     // 当client首次发消息给服务器时，OS会自动给client分配ip和port
    //     sendto(sock,message.c_str(),sizeof message,0,(struct sockaddr*)&server,sizeof server);
    //     struct sockaddr_in temp;
    //     socklen_t len = sizeof(temp);
    //     ssize_t s = recvfrom(sock,buffer,sizeof buffer,0,(struct sockaddr*)&temp,&len);
    //     if(s > 0)
    //     {
    //         buffer[s] = 0;
    //         std::cout << "server echo# " << buffer << std::endl;
    //     }

    // }
    // close(sock);
    return 0;
}